using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;

#nullable disable

namespace QXFramework.Net
{
    /// <summary>
    /// 描述客户端的对象
    /// </summary>
    public class ClientState
    {
        public uint guid;
        /// <summary>
        /// 客户端socket
        /// </summary>
        public Socket socket;
        /// <summary>
        /// 客户端的缓冲区
        /// </summary>
        public ByteArray readBuffer = new ByteArray();
        /// <summary>
        /// 上一次收到Ping的时间
        /// </summary>
        public long lastPingTime = 0;
    }

    /// <summary>
    /// 描述客户端的对象
    /// </summary>
    public class ServerState
    {
        /// <summary>
        /// 客户端socket
        /// </summary>
        public Socket socket;
        /// <summary>
        /// 客户端的缓冲区
        /// </summary>
        public ByteArray readBuffer = new ByteArray();
        /// <summary>
        /// 服务器类型
        /// </summary>
        public ServerType serverType;
        /// <summary>
        /// udp通信
        /// </summary>
        public UdpClient udpClient;
    }

    public static class ClientGuid
    {
        private static uint id = 0;
        private static HashSet<uint> ids = new HashSet<uint>();

        /// <summary>
        /// 分配Guid
        /// </summary>
        /// <returns></returns>
        public static uint GetGuid()
        {
            if (id == uint.MaxValue)
                id = 0;
            uint res = id++;
            while (ids.Contains(res))
            {
                if (id == uint.MaxValue)
                    id = 0;
                id++;
            }
            ids.Add(res);
            return res;
        }
    }

    public enum NetState
    {
        Close,
        ConnectSuccess,
        ConnectFailed,
    }

    public enum ServerType
    {
        Normal,
        Master,
        Slave,
    }
}